/*
 * Copyright (C), 2010-2017, ATCOM Tech. Co., Ltd.
 *
 * File Name: demo.c
 * Author: liaoxuecheng
 * mail: 872958436@qq.com
 * Created Time: Tue 08 Sep 2020 06:23:14 PM CST
 *
 */

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "threadpool.h"
#include "threadlog.h"

#define THIS_FILE "demo.c"

void func(void *arg)
{
    int *value = (int *)arg;
    DEBUG_INFO(THIS_FILE, "thread id:%lx arg: %d handing\n", pthread_self(), *(int *)(&value));
    usleep(rand()%1000 * 2000 + 1);
    DEBUG_INFO(THIS_FILE, "thread id:%lx arg: %d handing success\n", pthread_self(), *(int *)(&value));
    return;
    printf("thread id: %lx, arg: %d\n", pthread_self(), *(int *)(&value));
}

static int _test_method(int thread_cnt, int queue_size, int flags, int job_cnt, void(*function)(void*))
{
    int i;
    threadpool_t *pool;
    if(threadpool_create(&pool, thread_cnt, queue_size, flags) < 0)
    {
        DEBUG_ERROR(THIS_FILE, "threadpool_create error!");
        return -1;
    }

    sleep(1);

    for(i = 0; i < job_cnt; i++)
    {
        threadpool_add_job(pool, function, (void *)(*(int **)(&i)), 0);
    }
    usleep(10);
    if(threadpool_destory(pool) < 0)
    {
        DEBUG_ERROR(THIS_FILE, "threadpool_destory error!");
        return -1;
    }
    return 0;
}

int test_method(int thread_cnt, int queue_size, int flags, int job_cnt)
{
    DEBUG_ERROR(THIS_FILE, "job_cnt : %d", job_cnt);
    _test_method(thread_cnt, queue_size, flags, job_cnt, func);
}

#define SHOW_MSG() printf("please press enter key to test next demo!\n");getchar()


void func1(void *arg)
{
    usleep(12);
}

void leak_test()
{
    int i;
    threadpool_log_set_level(LOG_ERR);
    SHOW_MSG();
    for(i = 0; i < 100000; i++)
    {
        if(i % 10000 == 9999)
            printf("test count:%d\n", i+1);
        _test_method(8, 16, 0, 20, func1);
    }
    SHOW_MSG();
    for(i = 0; i < 100000; i++)
    {
        if(i % 10000 == 9999)
            printf("test count:%d\n", i+1);
        _test_method(8, 16, THREADPOOL_CLOSE_IMMEDIATELY, 20, func1);
    }
    SHOW_MSG();
    for(i = 0; i < 100000; i++)
    {
        if(i % 10000 == 9999)
            printf("test count:%d\n", i+1);
        _test_method(8, 16, THREADPOOL_CLOSE_IMDY_ANDABORT, 1000, func1);
    }
    SHOW_MSG();
}

int main(int argc, char **argv)
{
    srand(time(NULL));
    threadpool_log_init(THIS_FILE, THREAD_TERMLOG, LOG_DEBUG);

    //leak_test();
    test_method(2, 5, 0, 8);
    SHOW_MSG();

    threadpool_log_set_level(LOG_WARNING);
    test_method(DEFAULT_SIZE, DEFAULT_SIZE, THREADPOOL_MAX_QUEUE_SIZE | THREADPOOL_MIDDLE_THREADS, 5);
    SHOW_MSG();
    threadpool_log_set_level(LOG_NOTICE);
    test_method(DEFAULT_SIZE, DEFAULT_SIZE, 0, 10);
    SHOW_MSG();
    threadpool_log_set_level(LOG_WARNING);
    test_method(DEFAULT_SIZE, DEFAULT_SIZE, THREADPOOL_MAX_QUEUE_SIZE|THREADPOOL_MAX_THREADS|THREADPOOL_CLOSE_IMMEDIATELY, 500);
    SHOW_MSG();
    threadpool_log_set_level(LOG_WARNING);
    test_method(DEFAULT_SIZE, DEFAULT_SIZE, THREADPOOL_MAX_QUEUE_SIZE|THREADPOOL_MAX_THREADS|THREADPOOL_CLOSE_IMDY_ANDABORT, 50000);
}
